const api = require('../../network/api');
Page({

    /**
     * 页面的初始数据
     */
    data: {
        pageText: '',
        chapterList: [],
        chapter_id: '',
        scrollTop: 0,
        show: false,
        setShow: false,
        popup: false,
        size: 16,
        type: 'green',
        total: 0,
        colors: {
            'green': {
                frontColor: '#000000',
                backgroundColor: '#d2e3d0'
            },
            'sheepskin': {
                frontColor: '#000000',
                backgroundColor: '#f8eee4'
            },
            'pink': {
                frontColor: '#000000',
                backgroundColor: '#fce5df'
            },
            "black": {
                frontColor: '#ffffff',
                backgroundColor: '#000000'
            }
        },
        tars: [{
                icon: '/assets/img/directory.png',
                text: '目录'
            },
            {
                icon: '/assets/img/comments.png',
                text: '书友圈'
            }, {
                icon: '/assets/img/night.png',
                text: '夜间'
            }, {
                icon: '/assets/img/set.png',
                text: '设置'
            }
        ]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(option) {
        this.setColor(this.data.type)
        const {
            id,
            comic_id
        } = option
        console.log(18176, 'chapter_id', comic_id);
        this.init(id)
        this.getChpater(comic_id, id)
    },
    setColor(type) {
        wx.setNavigationBarColor({
            frontColor: this.data.colors[type].frontColor,
            backgroundColor: this.data.colors[type].backgroundColor,

        })
    },
    onClose() {
        this.setData({ popup: false });
    },
    async init(id) {
        const res = await api.getNovelChapterPage({
            id
        })
        if (res.code == 200) {
            this.setData({
                pageText: res.data,
                chapter_id: id
            })
        }
    },
    // 改变字体
    subtract(e) {
        const type = e.currentTarget.dataset.type
        console.log(type);
        let num = 0
        const size = this.data.size
        if (type == 2) {
            num = size - 1
            console.log(num, 'num');
            if (num < 12) {
                console.log('到底了');
                return
            }
        } else {
            num = size + 1

            if (num > 20) {
                // console.log('已经是最新章节');
                return
            }
        }
        this.setData({
            size: num
        })
    },
    // 获取章节列表
    async getChpater(comic_id, chapter_id) {
        const res = await api.novelDetail({ id: comic_id })
        if (res.code == 200) {
            console.log(res, '详情');
            const { read, price, charge } = res.data
            const user = wx.getStorageSync('userInfo')
            const param = {
                    comic_id,
                    chapter_id,
                    uid: user.id,
                    read,
                    price,
                    is_vip: charge
                }
                // const log = await api.addRead(param)
                // console.log(log, '添加情况');
        }
        const res1 = await api.getNovelChapter({
            comic_id,
            isAll: true
        })
        if (res1.code == 200) {
            const item = res1.data.data.find(item => item.id == chapter_id);
            const total = this.getPercent(item.title_alias, res1.data.data.length)
            console.log(total, '=====>');
            wx.setNavigationBarTitle({
                title: item.title,
            })
            this.setData({
                chapterList: res1.data.data,
                total
            })
        }
    },
    onChapter(e) {
        const id = e.currentTarget.dataset.id
        const item = this.data.chapterList.find(item => item.id == id)
        wx.setNavigationBarTitle({
            title: item.title,
        })
        this.setData({
            popup: false
        })
        this.init(item.id)

    },
    // 切换章节
    tabChapter(e) {
        const {
            id,
            type
        } = e.currentTarget.dataset
        console.log(id, type);
        const length = this.data.chapterList.length
        const index = this.data.chapterList.findIndex(item => item.id == id);
        let num = 0
        if (type == 1 && index != -1) {
            num = index - 1
            console.log(num, 'num');
            if (num < 0) {
                console.log('到底了');
                wx.showToast({
                    title: '当前是第一话',
                })
                return
            }
        } else {
            num = index + 1

            if (num >= length) {
                // console.log('已经是最新章节');
                wx.showToast({
                    title: '已经是最新章节',
                })
                return
            }
        }
        const item = this.data.chapterList[num]
        wx.setNavigationBarTitle({
            title: item.title,
        })
        console.log(num, item);
        this.init(item.id)
        this.getChpater(item.comic_id, item.id)
        this.scrollToTop()
    },
    // 切换不同页面
    onPage(e) {
        const id = e.currentTarget.dataset.id
        console.log(id, '==>');
        switch (id) {
            case 0:
                this.setData({
                    popup: true,
                    show: false
                })
                break
            case 2:
                this.setData({
                    type: 'black'
                })
                this.setColor('black')
                break
            case 3:
                this.setData({
                    setShow: !this.data.setShow
                })
                break
        }
    },
    selectColor(e) {
        const type = e.currentTarget.dataset.type
        const colors = this.data.colors
        const res = Object.keys(colors).find(item => colors[item].backgroundColor == type.backgroundColor)
        this.setData({
            type: res
        })
        this.setColor(res)


    },
    // 求百分比
    getPercent(num, total) {
        console.log(num, total);
        num = parseFloat(num);
        total = parseFloat(total);
        if (isNaN(num) || isNaN(total)) {
            return "-";
        }
        return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00);
    },
    async lower(e) {
        // let num = 0
        // const chapterList = this.data.chapterList
        // const index = chapterList.findIndex(item => item.id == this.data.chapter_id)
        // const id = chapterList[index + 1].id
        // const res = await api.getNovelChapterPage({
        //     id
        // })

        // console.log(id, 'scroll')
        // if (res.code == 200) {
        //     const pageText = this.data.pageText + `${chapterList[index + 1].title}\n ` +
        //         res.data
        //     this.setData({
        //         pageText,
        //         chapter_id: id
        //     })
        // }


    },
    scroll(e) {
        // console.log(e,'scroll')
    },
    scrollToTop() {
        this.setData({
            scrollTop: 0
        })
    },
    blocknav() {
        this.setData({
            show: !this.data.show
        })
    }
})